
	clear all

	global data "/Users/ingahei/UiO Dropbox/IngaB Heiland/Ais project/Repository/data_intermediate"
	global input "/Users/ingahei/UiO Dropbox/IngaB Heiland/Ais project/Repository/data_input"


	global cloud "/Users/ingahei/UiO Dropbox/IngaB Heiland/Desktop/comtrade_monthly/2016_d6"
	*global cloudU "/Users/inga/Desktop/comtrade_monthly"

	
	cd "$data"


local files : dir "$cloud" files "*.gz"
di `files'

global rlist `files'

foreach file in "$rlist" {

local fil = subinstr("`file'",".gz","",1)
local fi = subinstr("`file'",".gz",".txt",1) 

shell gunzip "$cloud/`file'" 
shell mv "$cloud/`fil'" "$cloud/`fi'"
import delim using "$cloud/`fi'", clear


capture noisily {  // a few files are empty
keep if partnercode==0 | partner2code==0
drop if partnercode==0 & partner2code==0
keep if flowcode=="X"                          // use only exports because of fob values
keep if customscode=="C00"
keep if motcode==0

drop if length(cmdcode)<6

keep period classificationcode cmdcode fobvalue reportercode partnercode partner2code netwgt
destring cmdcode, replace force

drop if partnercode==.	
sum reportercode
global reporter = r(max)
replace reporter = 381 if reporter==380 // Italy

if $reporter!=975 {
rename repor CountryCode
merge m:1 Co using "$input/countrycodes"
keep if _mer==3
drop _mer
rename ISO reporteriso
drop CountryCode
}

if $reporter==975 {
g reporteriso = "BTN"
drop reportercode
}

rename partnercod CountryCode
merge m:1 Co using "$input/countrycodes"
drop if _mer==2
drop _mer // for some reason, ITA has sometimes 380 and sometimes 381
replace ISO="ITA" if CountryCode==380
rename ISO partneriso
drop CountryCode

rename partner2cod CountryCode
merge m:1 Co using "$input/countrycodes"
drop if _mer==2
drop _mer // for some reason, ITA has sometimes 380 and sometimes 381
replace ISO="ITA" if CountryCode==380
rename ISO ndpartneriso
drop CountryCode

g fobvalue_co=fobvalue if partneriso=="WLD"
replace fobvalue = . if ndpartneriso!="WLD"

g netwgt_co=netwgt if partneriso=="WLD"
replace netwgt = . if ndpartneriso!="WLD"

g qfobvalue=fobvalue if netwgt>0 & netwgt!=.
g qfobvalue_co=fobvalue_co if netwgt_co>0 & netwgt_co!=.

replace netwgt = . if fobvalue==.
replace netwgt_co = . if fobvalue_co==.


drop if ndpar=="N/A" | ndpartneriso=="EU2" | ndpartneriso==""
drop if par=="N/A" | partneriso=="EU2" | partneriso==""

replace partneris=ndpartneris if partneriso=="WLD"
replace ndpartneris=partneris if ndpartneriso=="WLD"


gcollapse (firstnm) qfob* fob* netw*, by(period partner classification cmdcode reporter)

compress
sum period
global period = r(mean)
save "$cloud/comtrade_ct_${reporter}_${period}_6d", replace
}
global rlist "`ferest()'" // the list of unprocessed files
erase "$cloud/`fi'"
}


	local files : dir "$cloud" files "*_6d.dta"
	tempvar p
	
	foreach file in `files' {
		append using "$cloud/`file'"
	erase "$cloud/`file'"
	}
	save "$cloud/all_2016_d6", replace	
	
	
	

u "$cloud/all_2016_d6", clear

g post = 1 if period>201606
replace post=0 if post==.

* keep only reporters that use the same classification throughout 2016

g class = substr(classi,2,1)
destring class, replace
egen mclass=min(class), by(reporter)
g tag = 1 if mclass!=class
egen dropme=sum(tag), by(reporter)
drop if dropme!=0
drop dropme tag mclass class
replace qfobvalue_co = qfobvalue if qfobvalue_co==.
replace netwgt_co = netwgt if qfobvalue_co==. & qfobvalue!=.

g muv = qfobvalue/netwgt

gcollapse (mean) muv (sum) qfob* netw* (count) cmdcode, by(post partner reporter)

g uv = qfobvalue/netwgt
g uv_co = qfobvalue_co/netwgt_co
g lnuv = ln(uv)
g lnuv_co = ln(uv_co)
g lnN=ln(cmdcode)
g lnmuv=ln(muv)


rename reporter iso_i
rename partner iso_j
save temp, replace


u temp, clear
merge m:1 iso_i iso_j using "PESPbyHr_bil514" // PanExp1
keep if _m==3
drop _m
egen cid_i=group(iso_i)
egen cid_j=group(iso_j)

keep cid* lnuv lnmuv lnN XPan post

reshape wide lnuv lnN lnmuv, i(cid* XPan) j(post)

g dlnuv=lnuv1-lnuv0
g dlnmuv=lnmuv1-lnmuv0
g dlnN=lnN1-lnN0

reghdfe dlnu XPan, absorb(cid*) vce(robust)
reghdfe dlnmu XPan, absorb(cid*) vce(robust)
reghdfe dlnN XPan, absorb(cid*) vce(robust)


reghdfe dlnu XPan, absorb(cid*) cluster(cid*)
reghdfe dlnmu XPan, absorb(cid*) cluster(cid*)
reghdfe dlnN XPan, absorb(cid*) cluster(cid*)


















